<?php

namespace app\home\controller;

use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\Manager as ManagerModel;
use app\common\model\ManagerCompany as ManagerCompanyModel;


class Managerduty extends Base
{
    /**
     * [getAll  所有职务列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function all()
    {
        $param = input('param.');
        $map = [];
        $model = new ManagerDutyModel;
        $mcmodel = new ManagerCompanyModel;
        try{
            $map['comid'] = $param['comid'];
            $map['status'] = 1;
            $data = $model->getAllData($map,'id','id,duty');
            $moren = $model->limit(['comid'=>0],0,9,'id,duty');
            array_unshift($data,$moren);
            if(!empty($param['mid'])){
                $managercom = $mcmodel->findOne(['mid'=>$param['mid'],'comid'=>$param['comid']],'dutyid');
                if($managercom['dutyid'] == 0){
                    $managercom['dutyid'] = 2;
                }
                foreach($data[0] as &$v){
                    if($managercom['dutyid'] == $v['id']){
                        $v['checked'] = true;
                    }else{
                        $v['checked'] = false;
                    }
                }
            }

        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'获取成功',$data);
    }
    /**
     * [add  添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ManagerDutyModel;
        try{
            if($param['comid'] == 0){
                return admin_json(-1,'error');
            }
            $model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'添加成功');
    }
    /**
     * [edit  修改]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new ManagerDutyModel;
        try{
            if($param['id'] <= 9){
                return admin_json(-1,'error');
            }
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'修改成功');
    }
    /**
     * [is_duty  是否具有权限]
     * @param type 添加僧侣 12，2、编辑僧侣 13，3、解除僧侣权限 14，4、编辑权限组48，5、新增权限组47，6、编辑权限配置 46，7、修改密码 54
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function is_duty()
    {
        $param = input('param.');
        $model = new ManagerDutyModel;
        $mmodel = new ManagerModel;
        $mcmodel = new ManagerCompanyModel;
        $user = $this->user;
        try{
            $manager = $mmodel->findData($user['tel'],'tel','id');
            $mcdata = $mcmodel->findOne(['mid'=>$manager['id'],'comid'=>$param['comid'],'status'=>1],'dutyid');
            if($mcdata['dutyid'] == 2 || $mcdata['dutyid'] === 0){
                return admin_json(1,'成功',true);
            }elseif($mcdata['dutyid'] == 1 || empty($mcdata)){
                return admin_json(1,'成功',false);
            }
            $ddata = $model->findData($mcdata['dutyid'],'id','rules');
            $rules = explode(',',$ddata['rules']);
            $data = in_array($param['dutyid'],$rules);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'成功',$data);
    }
}